package com.dw.etl.core.controller;

import javax.servlet.http.HttpServletRequest;

import com.dw.etl.common.base.BaseException;
import com.dw.etl.core.bean.vo.LayMsg;
import org.springframework.beans.factory.annotation.Autowired;
import  com.dw.etl.common.base.BaseResult;
import  com.dw.etl.common.constant.CommonResultConstant;
import com.dw.etl.core.api.MdBaseDictItemService;
import com.dw.etl.core.bean.model.MdBaseDictItem;
import org.springframework.util.StringUtils;
import  com.dw.etl.common.vo.PaginationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * mdBaseDictItem控制器
 * Created by hozeData on 2021/07/13.
 */
@RestController
@RequestMapping("/etl/mdBaseDictItem/")
@Api(description="开发中", tags="开发中")
@CrossOrigin(origins = "*", maxAge = 3600)
public class MdBaseDictItemController {

    @Autowired
    MdBaseDictItemService mdBaseDictItemService;

    @ApiOperation(value="分页查询")
    @RequestMapping(value="findByPage", method=RequestMethod.POST)
    public BaseResult findByPage(@RequestBody MdBaseDictItem mdBaseDictItem, Integer pageNum, Integer pageSize) {
        PaginationVO<MdBaseDictItem> pageVO = mdBaseDictItemService.findPageByModel(mdBaseDictItem, pageNum, pageSize);
        return new BaseResult(CommonResultConstant.SUCCESS, pageVO);
    }

    @ApiOperation(value="插入")
    @ApiImplicitParam(name="mdBaseDictItem", value="实体", paramType="body", required=true, dataType="MdBaseDictItem")
    @RequestMapping(value="insert", method=RequestMethod.POST)
    public BaseResult insert(@RequestBody MdBaseDictItem mdBaseDictItem, HttpServletRequest request)  {
        int num = mdBaseDictItemService.insertSelective(mdBaseDictItem);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value="详情")
    @ApiImplicitParam(name="idDictItem", value="主键", paramType="query", required=true, dataType="String")
    @RequestMapping(value="getById", method=RequestMethod.GET)
    public BaseResult getById(String idDictItem, HttpServletRequest request) throws BaseException {
        if(StringUtils.isEmpty(idDictItem)){
            throw new BaseException("请选择要编辑的项");
        }
        MdBaseDictItem mdBaseDictItem = mdBaseDictItemService.getById(idDictItem);
        return new BaseResult(CommonResultConstant.SUCCESS, mdBaseDictItem);
    }

    @ApiOperation(value="修改")
    @ApiImplicitParam(name="mdBaseDictItem", value="实体", paramType="body", required=true, dataType="MdBaseDictItem")
    @RequestMapping(value="update", method=RequestMethod.POST)
    public BaseResult update(@RequestBody MdBaseDictItem mdBaseDictItem, HttpServletRequest request)  {
        int num = mdBaseDictItemService.updateByIdSelective(mdBaseDictItem);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value="根据主键删除")
    @ApiImplicitParam(name="idDictItem", value="主键", paramType="query", required=true, dataType="String")
    @RequestMapping(value="delete", method=RequestMethod.GET)
    public BaseResult delete(String idDictItem, HttpServletRequest request) throws BaseException {
        if(StringUtils.isEmpty(idDictItem)){
            throw new BaseException("请选择要删除的项");
        }
        int num = mdBaseDictItemService.deleteById(idDictItem);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }


    @ApiOperation(value="查询")
    @RequestMapping(value="find", method=RequestMethod.GET)
    public LayMsg findByPage(String idDict) {
        MdBaseDictItem mdBaseDictItem = new MdBaseDictItem();
        mdBaseDictItem.setIdDict(idDict);
        List<MdBaseDictItem> pageVO = mdBaseDictItemService.findByModel(mdBaseDictItem);
        return LayMsg.success().addData(pageVO);
    }
    @ApiOperation(value="保存")
    @ApiImplicitParam(name="mdBaseDictItem", value="实体", paramType="body", required=true, dataType="MdBaseDictItem")
    @RequestMapping(value="save", method=RequestMethod.POST)
    public BaseResult save(@RequestBody MdBaseDictItem mdBaseDictItem, HttpServletRequest request)  {
        int num;
        if(StringUtils.isEmpty(mdBaseDictItem.getIdDictItem())){
            num = mdBaseDictItemService.insertSelective(mdBaseDictItem);
        }else{
            num = mdBaseDictItemService.updateById(mdBaseDictItem);
        }
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

}
